<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    // 闭包
    function parents() {
      var a = 10;
      return function () {
        return a--;
      }
    }
    let a1 = parents();
    a1();
    a1();
    console.log(a1());
    let a2 = parents();
    console.log(a2());
    // var makeCounter = function () {
    //   var privateCounter = 0;

    //   function changeBy(val) {
    //     privateCounter += val;
    //   }
    //   return {
    //     increment: function () {
    //       changeBy(1);
    //     },
    //     decrement: function () {
    //       changeBy(-1);
    //     },
    //     value: function () {
    //       return privateCounter;
    //     }
    //   }
    // };

    // var Counter1 = makeCounter();
    // var Counter2 = makeCounter();
    // console.log('counter111', Counter1.value()); /* logs 0 */
    // Counter1.increment();
    // Counter1.increment();
    // console.log('counter111', Counter1.value()); /* logs 2 */
    // Counter1.decrement();
    // console.log('counter111', Counter1.value()); /* logs 1 */
    // console.log('counter222', Counter2.value()); /* logs 0 */
  </script>
</body>

</html>